package com.beidouapp.translator;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.beidouapp.cache.JbossCacheProvider;
import com.beidouapp.cache.impl.CacheTranslatorImpl;
import com.beidouapp.dao.RoleGroupDao;
import com.beidouapp.domain.pojo.RoleGroup;
import com.beidouapp.translate.Translator;

/**
 * 学员信息翻译器.
 */
@Component
public class RoleGroupTranslator extends CacheTranslatorImpl implements
		Translator {
	/**
	 * 日志记录器.
	 */
	private static final Logger logger = LoggerFactory
			.getLogger(RoleGroupTranslator.class);
	
	private JbossCacheProvider jbossCacheProvider;
	
	@Autowired(required=true)
	private RoleGroupDao roleGroupDao;

	public static final String fqn = TranslatorManager.DICT_CACHENODE_PATH
			+ "roleGroupTranslator";

	@Autowired
	public void setJbossCacheProvider(JbossCacheProvider jbossCacheProvider) {
		this.jbossCacheProvider = jbossCacheProvider;
		super.setJbossCacheProvider(jbossCacheProvider);
	}

	public RoleGroupDao getRoleGroupDao() {
		return roleGroupDao;
	}

	public void setRoleGroupDao(RoleGroupDao roleGroupDao) {
		this.roleGroupDao = roleGroupDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.carefx.framework.translate.Translator#getTranslatorName()
	 */
	@Override
	public String getTranslatorName() {
		return "roleGroupTranslator";
	}

	@Override
	public String translate(String fullQuilifiedName, String sourceFieldValue) {
		// sourceFieldValue代表要翻译的数据。我这里是将应用程序的ID翻译成应用
		// 程序的名称。
		if (StringUtils.isBlank(sourceFieldValue)) {
			return "";
		}
		Long id = null;
		try {
			id = Long.parseLong(sourceFieldValue);
		} catch (Exception e) {
			logger.warn("roleGroupTranslator数据翻译异常," + sourceFieldValue
					+ "不能转化成数字", e);
			return "";
		}
		RoleGroup roleGroup = roleGroupDao.getEntryByID(id);
		if (roleGroup == null) {
			return "";
		}
		return roleGroup.getGroupName();
	}

	public void removeCache(String sourceFieldValue) {
		jbossCacheProvider.removeCache(fqn, sourceFieldValue);
	}

	public boolean addItem(String sourceFieldValue, String targetFieldValue) {
		jbossCacheProvider.addCache(fqn, sourceFieldValue, targetFieldValue);
		return true;
	}

}
